package com.lxc.ms.service.serviceimpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxc.ms.dto.FenpaiDto;
import com.lxc.ms.dto.OrderPageDto;
import com.lxc.ms.entity.MsOrder;
import com.lxc.ms.mapper.DeptMapper;
import com.lxc.ms.mapper.OrderMapper;
import com.lxc.ms.result.PageResult;
import com.lxc.ms.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能:
 * 作者: lxc
 * 日期: 2024/8/29 14:55
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public void save(MsOrder msOrder) {
        List<MsOrder> list = orderMapper.selectAll();
        for (MsOrder order : list) {
            if(order.getOrderNo() == msOrder.getOrderNo()){
                return;
            }
        }
        orderMapper.save(msOrder);
    }

    @Override
    public void delete(Long id) {
        orderMapper.delete(id);
    }

    @Override
    public void update(MsOrder msOrder) {
        List<MsOrder> list = orderMapper.selectAll();
        for (MsOrder order : list) {
            if(order.getOrderNo() == msOrder.getOrderNo()){
                return;
            }
        }
        orderMapper.update(msOrder);
    }

    @Override
    public PageResult search(OrderPageDto orderPageDto) {
        PageHelper.startPage(orderPageDto.getPage(), orderPageDto.getPageSize());
        Page<MsOrder> page = orderMapper.search(orderPageDto);
        long total = page.getTotal();
        List<MsOrder> result = page.getResult();
        PageResult pageResult = new PageResult(total,result);
        return pageResult;
    }

    @Override
    public void fenpai(FenpaiDto fenpaiDto) {
        String deptName = fenpaiDto.getDeptName();
        Long deptId = deptMapper.findByDeptName(deptName);
        if(!deptId.equals(fenpaiDto.getHandleDeptId())){
            return;
        }
        orderMapper.fenpai(fenpaiDto);
    }
}
